<div class="form">

    <?php
    Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . "/js/jquery.number_format.js", CClientScript::POS_END);

    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'recipedetail-form',
        'enableAjaxValidation' => false,
            ));
    ?>
    <?php echo $form->errorSummary($model); ?>
    <?php echo $this->renderFlash(); ?>

    <table border="0" cellpadding="0" cellspacing="0"  id="id-form">             
        <tr>
            <th valign="top"><?php echo $form->labelEx($model, 'recipieid'); ?></th>
            <td>
                <?php
                echo menuService::listRecipies(Recipie::model()->findAll(), intval($_GET['id']));
                ?>
            </td>
            <td></td>
        </tr>
        <?php if (!$model->isNewRecord): ?>
            <tr>
                <th valign="top"><?php echo CHtml::label('Active', 'Active') ?></th>
                <td><?php echo CHtml::checkbox('active', $active, array()) ?>
                </td>
                <td></td>
            </tr>
        <?php endif; ?>
        <tr>
            <th valign="top"><?php echo $form->labelEx($model, 'syrupid'); ?></th>
            <td>
                <div style="height: 500px">      
                    <ul style="list-style-type: none">
                        <?php
                        //load preset material in recipie detail
                        if (count($presetRecipie)):
                            foreach ($presetRecipie as $preset => $presetVal):
                                $presetArray['material'][$presetVal->materialid] = $presetVal->materialquantity;
                                $presetArray['syrup'][$presetVal->syrupid] = $presetVal->syrupquantity;
                            endforeach;
                        endif;
                        
//                        var_dump($presetArray);
                        //syrup
                        foreach ($syrupList as $syrup => $value):
                            $preSyrupValue = 0;
                            if (isset($presetArray)):
                                $preSyrupValue = $presetArray['syrup'][$value->syrupid];
                            endif;
                            echo CHtml::openTag('li', array('style' => 'width: 500px; float:left; line-height: 10px; margin: 5px'));
                            ?>
                            <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                <?php
                                echo CHtml::openTag('th', array('valign' => 'top', 'style' => 'width: 200px'));
                                echo $value->syrupname;
                                echo CHtml::closeTag('th');
                                echo CHtml::openTag('td');
                                echo CHtml::textField('Syrup[quantity][' . $value->syrupid . ']', number_format($preSyrupValue, 0, '', ' '), array(
                                    'class' => 'inp-form',
                                    'onfocus' => '$(this).select()',
                                    'onkeyup' => '
                                                var format_number = $().number_format($(this).val().replace(/\s/g, ""), {
                                                    numberOfDecimals:0,
                                                    decimalSeparator: ",",
                                                    thousandSeparator: " "
                                                });

                                                $(this).val(format_number);
                                            ',
                                ));
                                echo CHtml::label(menuService::getUnitName($value->unitid), $value->unitid);                                
                                echo CHtml::closeTag('td');
                                ?>
                            </table>
                        <?php
                            echo CHtml::closeTag('li');
                        endforeach;
                        ?>
                    </ul>            
                </div>
            </td>
            <td></td>
        </tr>
        <tr>
            <th valign="top"><?php echo $form->labelEx($model, 'materialid'); ?></th>
            <td>
                <div style="height: 500px">      
                    <ul style="list-style-type: none">
                        <?php
                        //material
                        foreach ($materialList as $material => $value):
                            $preMaterialValue = 0;
                            if (isset($presetArray)):
                                $preMaterialValue = $presetArray['material'][$value->materialid];
                            endif;
                            echo CHtml::openTag('li', array('style' => 'width: 500px; float:left; line-height: 10px; margin: 5px'));
                            ?>
                            <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                <?php
                                echo CHtml::openTag('th', array('valign' => 'top', 'style' => 'width: 200px'));
                                echo $value->materialname;
                                echo CHtml::closeTag('th');
                                echo CHtml::openTag('td');
                                echo CHtml::textField('Material[quantity][' . $value->materialid . ']', number_format($preMaterialValue, 0, '', ' '), array(
                                    'class' => 'inp-form',
                                    'onfocus' => '$(this).select()',
                                    'onkeyup' => '
                                                var format_number = $().number_format($(this).val().replace(/\s/g, ""), {
                                                    numberOfDecimals:0,
                                                    decimalSeparator: ",",
                                                    thousandSeparator: " "
                                                });

                                                $(this).val(format_number);
                                            ',
                                ));
                                echo CHtml::label(menuService::getUnitName($value->unitid), $value->unitid);                                
                                echo CHtml::closeTag('td');
                                ?>
                            </table>
                           <?php
                            echo CHtml::closeTag('li');
                        endforeach;
                        ?>
                    </ul>            
                </div>
            </td>
            <td></td>
        </tr>        
        <tr>
            <th>&nbsp;</th>
            <td valign="top">
                <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class' => 'form-submit')); ?>
            </td>
            <td></td>
        </tr>
    </table>
    <?php $this->endWidget(); ?>

</div><!-- form -->